// pages/my/my.js
import { My } from '../my/my-model.js';
import { Order } from '../order/order-model.js';
import { Address } from '../../utils/address.js';

var my = new My();
var address = new Address();
var order = new Order();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    isLoadAll:false,//下拉到触底的时候检测时候还需要数据下拉出来
    orderArr:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._loadData();
    this._getAddressInfo();
  },

  onShow:function(){},

 /**用户信息 */
  _loadData:function(){
    //尝试自己改写
    // my.getUserInfo((res)=>{
    //   this.setData({
    //     userInfo:res
    //   })
    // })

    this._getOrderList();
  },

  /**获取我的地址*/
  _getAddressInfo:function(){
    address.getAddress((res)=>{
      this._bindAddrssInfo(res);
    })
  },

  _bindAddrssInfo:function(res){
    this.setData({
      addressInfo:res
    })
  },

  /**获取订单列表 */
  _getOrderList:function(){
    var that = this;
    var page = that.data.page;
    order.getOrderList(page,(res)=>{
      var data = res.data;
      if (data.length > 0) {
        that.data.orderArr.push.apply(that.data.orderArr, res.data);  //数组合并
        that.setData({
          orderArr: that.data.orderArr
        });
      } else {
        that.data.isLoadedAll = true;  //已经全部加载完毕
        that.data.pageIndex = 1;
      }
    })
  },

  onReachBottom:function(){
    if(!this.data.isLoadAll){
        this.data.page++;
        this.setData({
          page:this.data.page
        })
        this._getOrderList();
    }
  },

  /*显示订单的具体信息*/
  showOrderDetailInfo: function (event) {
    var id = this.getEvenData(event, 'id');
    wx.navigateTo({
      url: '../order/order?from=order&id=' + id
    });
  },


  /*获取e里的参数 */
  getEvenData: function (e, key) {
    return e.currentTarget.dataset[key];
  }


})